<?php
class IndexControl extends ModelModule
{
	public $layout = false;

	/**
	 * 模型列表
	 */
	public function actionModel()
	{
		$this->render();
	}

	/**
	 * 模型列表顶部菜单
	 */
	public function actionModelMenu($url)
	{
		$this->ajaxReturn($this->getCreateButton($url, '添加模型'), 'text');
	}

	/**
	 * 添加模型
	 */
	public function actionModelAdd()
	{
		$encrypt_key  = D::$config['encrypt'];
		if (isset($_POST['token'])) {
			if ($this->encrypt->decode($_POST['token'], $encrypt_key . '_addmodel') != 'add_model') {
				$this->ajaxReturn( $this->ajaxInfo(0, $this->msg['no_1']) );
			} else {
				modelModel::model()->insert();
				$this->ajaxReturn( $this->ajaxInfo(1, $this->msg['yes_3'], D::createUrl('model/index/model')) );
			}
		}
		//模板推送变量
		$var  = array(
			'token_code' => $this->encrypt->encode('add_model', $encrypt_key.'_addmodel'),
		);
		$this->render('model_add', $var);
	}

	/**
	 * 添加模型顶部菜单
	 */
	public function actionModelAddMenu($url)
	{
		$this->ajaxReturn($this->getSaveButton($url, 'add_model'), 'text');
	}

	public function actionModelEdit($mid = 0)
	{
		$encrypt_key  = D::$config['encrypt'];
		if (isset($_POST['token'])) {
			if ($this->encrypt->decode($_POST['token'], $encrypt_key . '_addmodel') != 'add_model') {
				$this->ajaxReturn( $this->ajaxInfo(0, $this->msg['no_1']) );
			} else {
				modelModel::model()->updateByPk($_POST['model_id']);
				$this->ajaxReturn( $this->ajaxInfo(1, $this->msg['yes_4'], D::createUrl('model/index/model')) );
			}
		} else {
			//模板推送变量
			$var  = array(
				'stauts'	 => true,
				'model'		 => modelModel::model()->getModelInfo((int)$mid),
				'token_code' => $this->encrypt->encode('add_model', $encrypt_key.'_addmodel'),
			);
			$this->render('model_add', $var);
		}
	}

	/**
	 * 添加模型顶部菜单
	 */
	public function actionModelEditMenu($url)
	{
		$this->ajaxReturn($this->getSaveButton($url, 'add_model'), 'text');
	}

	/**
	 * 字段管理
	 */
	public function actionField($mid = 0)
	{
		$this->assign('model', modelModel::model()->getModelInfo((int)$mid));
		$this->render();
	}

	/**
	 * 字段管理菜单
	 */
	public function actionFieldMenu($url, $url2 = '')
	{
		$this->ajaxReturn($this->getCreateButton($url, '添加字段', $url2), 'text');
	}

	/**
	 * 添加字段
	 */
	public function actionFieldAdd($mid = 0)
	{
		if (stripos($mid, 'menu') !== false) {
			$button  = $this->getSaveButton(D::createUrl('model/index/fieldAdd'), 'add_field');
			$button .= $this->getBackButton(D::createUrl('model/index/field'), D::createUrl('model/index/field', array('mid'=>1)));
			$this->ajaxReturn($button, 'text');
			exit;
		}
		$encrypt_key  = D::$config['encrypt'];
		$var = array(
			'token_code' => $this->encrypt->encode('add_field', $encrypt_key.'_addfield'),
		);
		$this->render('field_add', $var);
	}

}